/**
 * @fileOverview
 * @author iNahoo
 * @since 2018/5/3.
 */
"use strict";

import {createStore, combineReducers} from 'redux';
import {connect, Provider} from 'react-redux';
import React, {PureComponent, Component} from 'react';
import ReactDOM from 'react-dom';

const ACTIONS = {
    A_ADD: 'A_ADD',
    A_DESC: 'A_DESC',

    B_ADD: 'B_ADD',
    B_DESC: 'B_DESC',
};

const store = createStore(combineReducers({
    a: (v = 1, action) => {
        switch (action.type) {
            case ACTIONS.A_ADD: {
                return v + 1;
            }
            case ACTIONS.A_DESC: {
                return v + 1;
            }
            default:
                return v;
        }
    },
    b: (v = 1, action) => {
        switch (action.type) {
            case ACTIONS.B_ADD: {
                return v + 1;
            }
            case ACTIONS.B_DESC: {
                return v + 1;
            }
            default:
                return v;
        }
    },
}));


setImmediate(() => {
    store.dispatch({
        type: ACTIONS.A_ADD,
        a: 1
    });
});

class Main extends Component {
    render() {
        const {a, b, c, dispatch} = this.props;

        return <div>
            <div>{a}</div>
            <div>{b}</div>
            <div>{c}</div>
            <div onClick={() => {
                dispatch({
                    type: ACTIONS.B_ADD,
                    a: 1
                })
            }}>点我
            </div>
        </div>
    }
}

Main = connect(state => {
    return {
        ...state
    }
})(Main);

ReactDOM.render(
    <Provider store={store}><Main c={1}></Main></Provider>,
    document.getElementById('app'),
);
